<?php

App::uses('AppModel', 'Model');

/**
 * Pagina Model
 *
 * @property Usuario $Usuario
 */
class Pagina extends PaginasAppModel {

    public $actsAs = array('CakePtbr.AjusteUsuarios', 'CakePtbr.AjusteData', 'CakePtbr.AjusteFloat');
    public $name = 'Pagina';
    public $table = 'paginas';

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'titulo' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'default' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'status' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'ordem' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'exibir_menu' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'router' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Campo Obrigatorio',
            ),
            'duplicidade' => array(
                'rule' => array('duplicates')
            )
        ),
    );

    public function duplicates($check) {
        // $check will have value: array('promotion_code' => 'some-value')
        // $limit will have value: 25
        $router = $this->find('first', array(
            'conditions' => array('Pagina.router' => $check['router']),
        ));

        if (count($router) > 0) {
            if (!isset($this->data['Pagina']['id']) OR $router['Pagina']['id'] != $this->data['Pagina']['id']) {
                return "Rota já existe.";
            }
        }
        return TRUE;
    }

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Usuario' => array(
            'className' => 'Usuario',
            'foreignKey' => 'usuario_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $hasMany = array(
        'Conteudo' => array(
            'className' => 'Conteudo',
            'foreignKey' => 'pagina_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public function save($data = null, $validate = true, $fieldList = array()) {
        if (isset($data['Pagina']['default']) AND $data['Pagina']['default'] == 1) {
            parent::query('UPDATE paginas SET `default` = "0"');
        }
        if (isset($data['Pagina']['ordem']) AND $data['Pagina']['ordem'] == '') {
            $count = parent::find('count');
            $data['Pagina']['ordem'] = $count;
        }
        return parent::save($data, $validate, $fieldList);
    }

}
